var clientes = {
	init:function(){
		$.getJSON("ubigeos/listaubigeos", function(listaubigeos){
				setupGrid(listaubigeos);
			});
		var cliente = this;
		function setupGrid(listaubigeos){
			$('#clientes').jqGrid({
	           url:'clientes/index',
	           datatype:'json',
	           mtype:'POST',
	           colNames:['id','codigo','Razon social','Nombre Comercial','Direccion','Ruc','DNI','Telefono','Fax','Fecha Constitucion','Contacto','Ubigeo'],
	           colModel:[
	               {name:"data[Cliente][id]",hidden:true},
	               {name:"data[Cliente][codigo]",index:'Cliente.codigo',align:"rigth" ,width:30,sortable:true,editable:true,editoptions:{size:10,maxlength:5} ,editrules:{required:true,number:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
				   {name:"data[Cliente][razonsocial]",index:'Cliente.razonsocial',editoptions:{size:40, maxlength:60},width:100,align:'left',sortable:true,editable:true,editrules:{required:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
	               {name:"data[Cliente][nombrecomercial]",index:'Cliente.nombrecomercial',editoptions:{size:40, maxlength:60},hidden:true,width:100,align:'left',sortable:true,editrules:{edithidden:true},editable:true,searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
				   {name:"data[Cliente][direccion]",index:'Cliente.direccion',width:60,align:'left',editoptions:{size:40, maxlength:250},sortable:true,editable:true,searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
				   {name:"data[Cliente][ruc]",index:'Cliente.ruc',width:30,align:'left',sortable:true,editable:true,editoptions:{maxlength:11},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
				   {name:"data[Cliente][dni]",index:'Cliente.dni',hidden:true,width:60,align:'left',sortable:true,editable:true,editoptions:{maxlength:8},editrules:{edithidden:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
				   {name:"data[Cliente][telefono]",index:'Cliente.telefono',width:30,align:'left',sortable:true,editoptions:{maxlength:12},editable:true,searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
				   {name:"data[Cliente][fax]",index:'Cliente.fax',hidden:true,width:60,align:'left',sortable:true,editable:true,editoptions:{maxlength:12},editrules:{edithidden:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
				   {name:"data[Cliente][fechaconstitucion]",index:'Cliente.fechaconstitucion',hidden:true,width:60,align:'left',sortable:true,editable:true,editrules:{edithidden:true, date:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
				   {name:"data[Cliente][contactocliente]",index:'Cliente.contactocliente',hidden:true,width:60,editoptions:{size:40, maxlength:250},align:'left',sortable:true,editable:true,editrules:{edithidden:true},searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}},
				   {name:"data[Cliente][ubigeo_id]",index:'Ubigeo.distrito',width:45,align:'left',sortable:true,editable:true,editoptions:{size:40, value:listaubigeos},edittype:"select",searchoptions:{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew']}}           
	           ],
			 
			   pager:$('#clientes-pager'),
	           rowNum:10,
	           rowList:[10,20,30],
	           mtype: "POST",
	           viewrecords:true,
	           caption:'Clientes',
	           height:'180',
	           sortname: "Cliente.razonsocial",
	           editurl: 'clientes/index'
	          
	                
	       });
	       $('#clientes').navGrid("#clientes-pager",{edit:true,add:true,del:true},{width:390,clearAfterEdit:false,reloadAfterSubmit:true, closeOnEscape:true, afterSubmit:cliente.afterSubmit},{width:390,clearAfterAdd:true,reloadAfterSubmit:true, closeOnEscape:true,afterSubmit:cliente.afterSubmit},{width:270,reloadAfterSubmit:true, closeOnEscape:true},{sopt:['cn','eq','ne','lt','le','gt','ge','bw','ew'],multipleSearch:true, closeOnEscape:true});
	       $("#clientes").jqGrid('filterToolbar',{stringResult: true,searchOnEnter : false});
	       jQuery("#clientes").jqGrid('setGridWidth',$('#RightPane').width()-50);
		}
	},
	afterSubmit: function(response){ var respuesta = JSON.parse(response.responseText); return [respuesta.respuesta,respuesta.mensaje];}
};